using System;
using BitMango;
using SevenPieceGame;
using UnityEngine;

public class StorePopupHandler : MonoBehaviour, IPopupWnd, IHandlerBase
{
	public void DoDataExchange()
	{
		if (Wallet.GetItemCount("adfree") > 0)
		{
			this.removeads.SetActive(false);
			this.removeadsdisable.SetActive(false);
		}
		else
		{
			this.removeads.SetActive(false);
			this.removeadsdisable.SetActive(false);
		}
	}

	public void OnEnterPopup(object param)
	{
		this.restoreBtn.SetActive(Context.BMPlatform.storeType == PlatformContext.StoreType.AppleStore);
	}

	public void OnLeavePopup()
	{
	}

	private void OnClickRestore()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Scene.Lock("Loading");
		//StoreService.ProcessRestoreAllPurchases(delegate(bool success)
		//{
		//	if (success)
		//	{
		//		SyncPackage.Sync();
		//	}
		//	Scene.Unlock();
		//});
	}

	private void OnClickRemoveads()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		if (Wallet.GetItemCount("adfree") > 0)
		{
			return;
		}
		ShopItem shopItemByID = Context.UncleBill.GetShopItemByID("adfree");
		Scene.Lock("Loading");
		//StoreService.ProcessBuyItem(shopItemByID, delegate(bool succese)
		//{
		//	Scene.Unlock();
		//	if (succese)
		//	{
		//		this.removeads.SetActive(false);
		//		this.removeadsdisable.SetActive(true);
		//		this.BuyLog("adfree");
		//	}
		//});
	}

	private void OnClickCoin240(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("1");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
        {
            if (PurchaseResult == PurchaseResult.Success)
            {
                //够次数更新payingItem的数据
                MainManager.Instance.AddDoller(19);
            }
        });  
	}

	private void OnClickCoin800(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("2");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(59);
			}
		});  
	}

	private void OnClickCoin1400(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("3");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(99);
			}
		});  
	}

	private void OnClickCoin3000(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("4");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(159);
			}
		});  
	}

	private void OnClickCoin7200(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("5");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(299);
			}
		});  
	}

	private void OnClickCoin9200(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("6");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(499);
			}
		});  
	}
	
	private void OnClickCoin10800(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("7");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(599);
			}
		});  
	}
	
	private void OnClickCoin12800(tk2dUIItem obj)
	{
		CfgShop table = ConfigManager.Instance.GetShopData("8");
		DataIAP.Instance.Purchase(table.IAP_Key,(PurchaseResult PurchaseResult, string iapKey) =>
		{
			if (PurchaseResult == PurchaseResult.Success)
			{
				//够次数更新payingItem的数据
				MainManager.Instance.AddDoller(799);
			}
		});  
	}

	private void BuyCoin(string name, tk2dUIItem obj)
	{
		ShopItem item = Context.UncleBill.GetShopItemByID(name);
		Scene.Lock("Loading");
		//StoreService.ProcessBuyItem(item, delegate(bool succese)
		//{
		//	if (succese)
		//	{
		//		this.BuyLog(name);
		//		GameObject gameObject = obj.transform.parent.transform.Find("icon").gameObject;
		//		this.coinInfo.CoinAnimation(this.cam.transform.InverseTransformPoint(gameObject.transform.position), 5, 0.15f, item.rewards[0].count, delegate
		//		{
		//		});
		//	}
		//	Scene.Unlock();
		//});
	}

	private void BuyLog(string name)
	{
		DateTime d = DateTime.UtcNow;
		if (PlayerPrefs.HasKey("installed"))
		{
			d = DateTime.Parse(PlayerPrefs.GetString("installed"));
		}
		DWATTHelper.logEvent("BuyItemDay", new object[]
		{
			name,
			(DateTime.Now - d).Days
		});
	}

	private void OnClickClose()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Scene.ClosePopup("StorePopup", null);
	}

	private void OnEscape()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Scene.ClosePopup("StorePopup", null);
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public GameObject removeads;

	public GameObject removeadsdisable;

	public TopOverlayHandler coinInfo;

	public GameObject restoreBtn;
}
